package com.coocaa.ops.admin.v1.module.convert;

import com.baomidou.mybatisplus.annotation.TableField;
import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.module.util.ModuleOnclickUtil;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.biz.dmp.entity.DmpRegistryRlsInfoEntity;
import com.coocaa.ops.common.core.biz.homepage.entity.HomeModuleContentConfigInfo;
import com.coocaa.ops.common.core.biz.homepage.entity.HomepageInfoEntity;
import com.coocaa.ops.common.core.biz.module.entity.ModuleInfoEntity;
import com.coocaa.ops.common.core.biz.module.entity.ModuleTabContentConfigInfo;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

/**
 * @author bijiahao
 * @description
 * @date 2020/6/16 16:30
 */
public class ModuleSaveConverter {

    /**
     * 保存前处理
     *
     * @param entity
     */
    public static void handleBeforeSave(ModuleInfoEntity entity) {
        //是否有dmp内容设置
        handleSetHasDmp(entity);
    }

    public static void setDefaultValue(ModuleInfoEntity entity) {
        entity.setOnclick(ModuleOnclickUtil.getFinalOnclick(entity));

    }

    public static DmpRegistryRlsInfoEntity getDmpRegistryRlsInfoEntity(ModuleInfoEntity entity, ModuleTabContentConfigInfo moduleTabContentConfigInfo){
        DmpRegistryRlsInfoEntity dmpRegistryRlsInfoEntity = moduleTabContentConfigInfo.getDmpRegistryInfo();
        dmpRegistryRlsInfoEntity.setParentContainerId(entity.getId());
        dmpRegistryRlsInfoEntity.setParentContainerVersion(entity.getCurrentVersion());
        dmpRegistryRlsInfoEntity.setParentContainerType(ResourceTypeEnum.MODULE.getValue());
        dmpRegistryRlsInfoEntity.setSubContainerId(moduleTabContentConfigInfo.getId());
        dmpRegistryRlsInfoEntity.setSubContainerType(ResourceTypeEnum.TAB.getValue());
        dmpRegistryRlsInfoEntity.setCreator(entity.getModifier());
        dmpRegistryRlsInfoEntity.setModifier(entity.getModifier());
        return  dmpRegistryRlsInfoEntity;
    }


    private static void handleSetHasDmp(ModuleInfoEntity entity){
        boolean isHasDmp  =  isHasDmp(entity);
        entity.setHasDmp(isHasDmp ? 1: 0);
    }


    private static boolean isHasDmp(ModuleInfoEntity entity){
        List<ModuleTabContentConfigInfo> topTabDmpList = entity.getTopTabDmpList();
        List<ModuleTabContentConfigInfo> bottomTabDmpList = entity.getBottomTabDmpList();
        return isDmpAvailable(topTabDmpList) || isDmpAvailable(bottomTabDmpList);
    }

    private static boolean isDmpAvailable(List<ModuleTabContentConfigInfo> tabDmpList){
        if (CollectionUtils.isEmpty(tabDmpList)){
            return false;
        }
        for (ModuleTabContentConfigInfo moduleTabContentConfigInfo : tabDmpList){
            if (InputParamsValidator.isDmpContentAvailable(moduleTabContentConfigInfo.getId(), moduleTabContentConfigInfo.getDmpRegistryInfo())){
                 return true;
            }
        }
        return false;
    }

}
